package screens 
{
	import Buttons.PlayNoteButton;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import Entities.Note;
	import Entities.InstrumentLayer;
	import flash.events.KeyboardEvent;
	
	/**
	 * ...
	 * @author Andy Wolff
	 */
	public class PlayNoteTab extends MovieClip
	{
		[Embed(source = '../images/Buttons/tabs2.png')] private var tab2IMG:Class;
		
		[Embed(source = '../images/Buttons/aButton.png')] private var aButtonIMG:Class;
		[Embed(source = '../images/Buttons/bButton.png')] private var bButtonIMG:Class;
		[Embed(source = '../images/Buttons/cButton.png')] private var cButtonIMG:Class;
		[Embed(source = '../images/Buttons/dButton.png')] private var dButtonIMG:Class;
		[Embed(source = '../images/Buttons/eButton.png')] private var eButtonIMG:Class;
		[Embed(source = '../images/Buttons/fButton.png')] private var fButtonIMG:Class;
		[Embed(source = '../images/Buttons/gButton.png')] private var gButtonIMG:Class;
		
		private var canvas:NoteCanvas;
		private var previousNotes:Array;
		public function makeNote(which:int):void {
			if (previousNotes[which] != null && NoteCanvas.playPos >= previousNotes[which].getEnd() && NoteCanvas.playPos <= previousNotes[which].getEnd()+4) {
				previousNotes[which].extend(new Point(NoteCanvas.playPos, 0));
			}
			else { canvas.saveState(); previousNotes[which] = new Note(canvas.getCurrentInstrument(), which, NoteCanvas.playPos, NoteCanvas.playPos); canvas.getCurrentInstrument().addNote(previousNotes[which]); }
			
		}
		
		private var buttons:Array;
		
		public function PlayNoteTab(canvas:NoteCanvas) 
		{
			this.canvas = canvas;
			
			addChild(new tab2IMG());
			
			previousNotes = new Array(8);
			
			var images:Array = new Array(new cButtonIMG(), new dButtonIMG(), new eButtonIMG(), new fButtonIMG(), new gButtonIMG(), new aButtonIMG(), new bButtonIMG(), new cButtonIMG());
			buttons = new Array();
			for (var i:int = 0; i < 8; i++) {
				var btn:PlayNoteButton = new PlayNoteButton(canvas, this, i);
				addChild(btn);
				buttons.push(btn);
				btn.addChild(images[i]);
				btn.x = 30;
				if (i == 0) btn.y = 35; 
				else btn.y = buttons[i - 1].y + buttons[i - 1].height + 5; 
			}
			
			Main.instance.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			Main.instance.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
			addEventListener(Event.EXIT_FRAME, endstep);
			addEventListener(Event.REMOVED, kill);		}
		
		public function endstep(e:Event):void {
			var anyPressed:Boolean = false;
			for (var i:int = 0; i < 8; i++) if (buttons[i].held || buttons[i].keyHeld) anyPressed = true;
			//if (!anyPressed) canvas.getCurrentInstrument().unify();
			if (anyPressed) canvas.fastForward(4);
		}
		public function kill(e:Event):void { 
			Main.instance.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			Main.instance.stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
		}
		public function keyDown(k:KeyboardEvent):void {
			var which:int = k.keyCode-49;
			if (which>=0 && which<=7) buttons[k.keyCode-49].keyDown(k);
		}
		public function keyUp(k:KeyboardEvent):void {
			var which:int = k.keyCode-49;
			if (which>=0 && which<=7) buttons[k.keyCode-49].keyUp(k);
		}
		
	}

}